package co.za.itweb.securedata;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;



@SuppressLint({ "SetJavaScriptEnabled"})
public class DialogActivity extends Activity {
	
	public ProgressDialog pd;
	
	private WebView myWebView;
    ArrayList<String>lista;
	String marker;
	private String density;
	private int mapwidth;
	int width;
	int height;
	String address;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		
		
		super.onCreate(savedInstanceState);
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		setContentView(R.layout.map_dialog);
		
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		height = displaymetrics.heightPixels;
	     width = displaymetrics.widthPixels;
		RelativeLayout googleMap = (RelativeLayout) findViewById(R.id.web);

		
			RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(width,height);

			marker="";
			
				/*
						try {
							JSONArray a= First.GPSMARKERS.getJSONArray("office");
							
							for(int h=0;h<a.length();h++){
								JSONObject row = a.getJSONObject(h);
								markers+="['"+row.getString("address")+"','"+row.getString("latitude")+"','"+row.getString("longitude")+"','"+(h+1)+"'],";
															}
						} catch (JSONException e1) {
						
							e1.printStackTrace();
						}

			*/
			
			try {
				JSONObject a= First.GPSMARKERS.getJSONObject("office");
				
				
					//JSONObject row = a.getJSONObject(h);
					//marker+="['"+a.getString("latitude")+"','"+a.getString("longitude")+"']";//'"+a.getString("address")+"',,'"+(h+1)+"'
				   marker+=a.getString("latitude")+","+a.getString("longitude");
				    address=a.getString("address");
			} catch (JSONException e1) {
			
				e1.printStackTrace();
			}
						//markers.charAt(markers.length() - 1);
			//markers=markers.substring(0, markers.lastIndexOf(",")); 
			//marker=markers.substring(0, markers.length() - 1); 
			//marker+="];";
			
			
		
			
			
			 myWebView = new WebView(getApplicationContext()); 
			
			  int mapHeight =height-180;
		 
			  
		

		
		googleMap.addView(myWebView, layoutParams);
	
		
	
		
		myWebView.getSettings().setJavaScriptEnabled(true);
		myWebView.getSettings().setUseWideViewPort(true);

		
		if(determineDensity()=="ldpi"){
			mapwidth=(int) (width+(width/3)-15);
			mapHeight=(int) (height+(height/3)-180);
		}else if(determineDensity()=="mdpi"){
			mapwidth=width-20;
			mapHeight=height-160;
		}else if(determineDensity()=="hdpi"){
			mapwidth=(int) width-(width/3)-15;
			mapHeight=(int) (height)-(height/3)-140;
		}else{
			mapwidth=(int) (width/2)-15;
			mapHeight=(int) (height)-(height/2)-120;
		}
		
		
		
		
		
	
		/*
		 String htmlString ="<html>"
"<head>"+ 
  "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />"+ 

  "<script src=\"http://maps.google.com/maps/api/js?sensor=false\""+ 
          "type=\"text/javascript\"></script>"+
"</head>"+ 
"<body>"+
  "<div id=\"map\" style=\"width:"+mapwidth+"px;height:"+mapHeight+"px;\"\"></div>"+

  "<script type=\"text/javascript\">"+ markers+
//  "var locations = ["+
//        " ['Nicol Main Office Park', -26.05414, 28.02228, 1],"+
//         " ['The Avalon Building', -33.933832, 18.417271, 2],"+
//         " ['10 Derby Place, Doncaster  Derby Downs Office Park', -29.822002, 30.947475, 3]"+
//      
//       " ];"+

   
   " var map = new google.maps.Map(document.getElementById('map'), {"+
      "zoom: 4,"+
      "center: new google.maps.LatLng(0, 0),"+
      "mapTypeId: google.maps.MapTypeId.ROADMAP"+
    "});"+
   
    "var infowindow = new google.maps.InfoWindow();"+

    "var marker, i;"+
    "var bounds = new google.maps.LatLngBounds();"+
    "for (i = 0; i < locations.length; i++) {"+  
    "var myLatLng =new google.maps.LatLng(locations[i][1], locations[i][2]);"+
      "marker = new google.maps.Marker({"+
        "position: new google.maps.LatLng(locations[i][1], locations[i][2]),"+
        
        "map: map"+
       
      "});"+
      
 "bounds.extend(myLatLng);"+
 "map.fitBounds(bounds);"+
     
      "google.maps.event.addListener(marker, 'click', (function(marker, i) {"+
        "return function() {"+
          "infowindow.setContent(locations[i][0]);"+
          "infowindow.open(map, marker);"+
        "}"+
      "})(marker, i));"+
    "}"+
  "</script>"+
"</body>"+
"</html>";
		 */
		
		 String htmlString ="<html>"+
				 "<head>"+ 
				 "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />"+ 

  				"<script src=\"http://maps.google.com/maps/api/js?sensor=false\""+ 
  				" type=\"text/javascript\"></script>"+
  				"</head>"+ 
  				"<body>"+
  				"<div id=\"map\" style=\"width:"+mapwidth+"px;height:"+mapHeight+"px;\"></div>" +
  				"<script type=\"text/javascript\">"+
		    "var position = new google.maps.LatLng("+marker+");"+
		    "var myOptions = {"+
		      "zoom: 15,"+
		      "center: position,"+
		      "mapTypeId: google.maps.MapTypeId.ROADMAP"+
		    "};"+
		    "var map = new google.maps.Map("+
		        "document.getElementById(\"map\"),"+
		        "myOptions);"+
		 
		    "var marker = new google.maps.Marker({"+
		        "position: position,"+
		        "map: map,"+
		        "title:\"Secure Data\""+
		    "});"+  
		 
		    "var contentString = '"+address+"';"+
		    "var infowindow = new google.maps.InfoWindow({"+
		        "content: contentString"+
		    "});"+
		 
		    "google.maps.event.addListener(marker, 'click', function() {"+
		      "infowindow.open(map,marker);"+
		    "});"+
		 
		  
		 
		"</script>" +
		"</body>"+
		"</html>";
	
		

		myWebView.loadData(htmlString, "text/html", "utf-8");
		

		 Button buttonclose = (Button) findViewById(R.id.closemap);
			buttonclose.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					finish();
				}
			});
			ScrollView scrollIt=(ScrollView) findViewById(R.id.scrollIt);
			scrollIt.setScrollbarFadingEnabled(false);
			TextView ext=(TextView) findViewById(R.id.infotext);
			String infostring="";
			try {
				JSONArray a= First.INFO.getJSONArray("office");
				
				for(int h=0;h<a.length();h++){
					JSONObject row = a.getJSONObject(h);
					infostring+=row.getString("address").replace("\\n", "\n")+"\n"+row.getString("contact").replace("\\n", "\n");
												}
			} catch (JSONException e1) {
			
				e1.printStackTrace();
			}
			ext.setText(infostring);
		
			
	}
	
	
		

	public String determineDensity(){
		switch (getResources().getDisplayMetrics().densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			density="ldpi";
			 
		    break;
		case DisplayMetrics.DENSITY_MEDIUM:
			density= "mdpi";
			 
			 break;
		case DisplayMetrics.DENSITY_HIGH:
			density= "hdpi";
			
			 break;
		case DisplayMetrics.DENSITY_XHIGH:
			density= "xdpi";
			
			 break;
		}
		return density;
	}
	
	

}
